package com.huslee.shark.log.log4j2.plugins;

import com.huslee.shark.log.log4j2.event.ElkLog4jEvent;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.async.AsyncLoggerConfig;
import org.apache.logging.log4j.core.config.*;
import org.apache.logging.log4j.core.config.plugins.*;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.apache.logging.log4j.util.Strings;

import java.util.Arrays;
import java.util.List;

/**
 * @author: zhanglin574@xdf.cn
 * @date: 2021/8/18
 * @description:
 */
@Plugin(name = "ELkAsyncLogger", category = Node.CATEGORY, printObject = true)
public class ElkAsyncLoggerConfig extends AsyncLoggerConfig {
    public ElkAsyncLoggerConfig(String name, List<AppenderRef> appenders, Filter filter, Level level, boolean additive, Property[] properties, Configuration config, boolean includeLocation) {
        super(name, appenders, filter, level, additive, properties, config, includeLocation);
    }

    @PluginFactory
    public static LoggerConfig createLogger(
            @PluginAttribute(value = "additivity", defaultBoolean = true) final boolean additivity,
            @PluginAttribute("level") final Level level,
            @Required(message = "Loggers cannot be configured without a name") @PluginAttribute("name") final String loggerName,
            @PluginAttribute("includeLocation") final String includeLocation,
            @PluginElement("AppenderRef") final AppenderRef[] refs,
            @PluginElement("Properties") final Property[] properties,
            @PluginConfiguration final Configuration config,
            @PluginElement("Filter") final Filter filter) {
        final String name = loggerName.equals(ROOT) ? Strings.EMPTY : loggerName;
        return new ElkAsyncLoggerConfig(name, Arrays.asList(refs), filter, level, additivity, properties, config,
                includeLocation(includeLocation));
    }
    public void log(final LogEvent event, final LoggerConfigPredicate predicate){
        ElkLog4jEvent elkEvent = null;
        if( !(event instanceof  ElkLog4jEvent)) {
            elkEvent = new ElkLog4jEvent(event);
        }else{
            elkEvent = (ElkLog4jEvent) event;
        }
        super.log(elkEvent,predicate);
    }
}
